/**
 * Project Name:zyhj-admin
 * File Name:YjfkxxServiceImpl.java
 * Package Name:com.vviton.zyhj.admin.service.impl
 * Date:2016年11月29日上午10:41:57
 *
 */

package com.vviton.zyhj.admin.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.vviton.zyhj.admin.service.YjfkxxService;
import com.vviton.zyhj.common.base.AppConfig;
import com.vviton.zyhj.common.base.BaseService;
import com.vviton.zyhj.common.common.CodeConstants;
import com.vviton.zyhj.common.common.ExcelConstants;
import com.vviton.zyhj.common.common.GlobalConstants;
import com.vviton.zyhj.common.common.ValidatorConstants;
import com.vviton.zyhj.common.dao.TYyxtO2oYjfkxxMapper;
import com.vviton.zyhj.common.entity.TYyxtCode;
import com.vviton.zyhj.common.entity.TYyxtO2oYjfkxx;
import com.vviton.zyhj.common.utils.DateUtils;
import com.vviton.zyhj.common.utils.ExcelUtil;
import com.vviton.zyhj.common.utils.FileBean;
import com.vviton.zyhj.common.utils.ResultJson;
import com.vviton.zyhj.common.utils.page.Page;
import com.vviton.zyhj.common.view.ParamYjfkxx;
import com.vviton.zyhj.common.view.ViewYjfkxx;

/**
 * ClassName:YjfkxxServiceImpl <br/>
 * Function: 意见反馈Service实现类. <br/>
 * Date: 2016年11月29日 上午10:41:57 <br/>
 *
 * @author libo
 * @version
 * @since JDK 1.7
 * @see
 */
public class YjfkxxServiceImpl extends BaseService implements YjfkxxService {

    /** appConfig */
    @Autowired
    private AppConfig appConfig;

    /** o2o意见反馈DAO */
    @Autowired
    private TYyxtO2oYjfkxxMapper tYyxtO2oYjfkxxMapper;

    /**
     *
     * selectYjfkxxListByPage,(分页查询意见反馈列表). <br/>
     * Author: libo <br/>
     * Create Date: 2016年11月29日 <br/>
     * ===============================================================<br/>
     * Modifier: libo <br/>
     * Modify Date: 2016年11月29日 <br/>
     * Modify Description: <br/>
     * ===============================================================<br/>
     *
     * @param paramYjfkxx 意见反馈参数对象
     * @return Page
     * @since JDK 1.7
     */
    @Override
    public Page selectYjfkxxListByPage(ParamYjfkxx paramYjfkxx) {
        Page page = new Page();
        page.setCurrPage(paramYjfkxx.getCurrPage());
        page.setPageSize(paramYjfkxx.getPageSize());
        page.setParam(paramYjfkxx);
        List<TYyxtO2oYjfkxx> yjfkxxList = tYyxtO2oYjfkxxMapper.selectFkxxListByPage(page);
        List<ViewYjfkxx> yjfkxxViews = new ArrayList<ViewYjfkxx>();

        Map<String, TYyxtCode> wtlxMap = this.getCodeMapCodeclass(CodeConstants.WTLX);
        Map<String, TYyxtCode> wtztMap = this.getCodeMapCodeclass(CodeConstants.WTZT);
        TYyxtCode tYyxtCode = null;
        for (TYyxtO2oYjfkxx fkxx : yjfkxxList) {
            ViewYjfkxx yjfkxx = new ViewYjfkxx();
            yjfkxx.setId(fkxx.getId());
            yjfkxx.setFkrmc(fkxx.getFkrmc());
            yjfkxx.setTcrdh(fkxx.getTcrdh());
            yjfkxx.setTcrjg(fkxx.getTcrjg());
            yjfkxx.setTcrmc(fkxx.getTcrmc());
            yjfkxx.setTcsj(fkxx.getTcsj());
            yjfkxx.setWtlx(fkxx.getWtlx());
            yjfkxx.setWtnr(fkxx.getWtnr());
            yjfkxx.setWtzt(fkxx.getWtzt());
            yjfkxx.setBt(fkxx.getBt());
            //处理状态描述
            tYyxtCode = wtlxMap.get(CodeConstants.WTLX + fkxx.getWtlx());
            if (tYyxtCode != null) {
                yjfkxx.setWtlxDesc(tYyxtCode.getCodedesc());
            }
            tYyxtCode = wtztMap.get(CodeConstants.WTZT + fkxx.getWtzt());
            if (tYyxtCode != null) {
                yjfkxx.setWtztDesc(tYyxtCode.getCodedesc());
            }
            yjfkxxViews.add(yjfkxx);
        }
        page.setData(yjfkxxViews);

        int total = tYyxtO2oYjfkxxMapper.countFkxxListTotal(page);
        page.setTotal(total);

        return page;
    }

    /**
     *
     * getFkxxIds,(查询所有的意见反馈的id). <br/>
     * Author: libo <br/>
     * Create Date: 2016年12月2日 <br/>
     * ===============================================================<br/>
     * Modifier: libo <br/>
     * Modify Date: 2016年12月2日 <br/>
     * Modify Description: <br/>
     * ===============================================================<br/>
     *
     * @param paramYjfkxx 意见反馈参数对象
     * @return List<String>
     * @since JDK 1.7
     */
    @Override
    public List<String> getFkxxIds(ParamYjfkxx paramYjfkxx) {
        List<ViewYjfkxx> fkxxViews = tYyxtO2oYjfkxxMapper.getFkxxIds(paramYjfkxx);
        List<String> idsList = new ArrayList<String>();
        for (ViewYjfkxx fkxx : fkxxViews) {
            idsList.add(fkxx.getId());
        }
        return idsList;
    }

    /**
     *
     * getFkxxListByIds,(EXCEL导出选中的意见反馈列表). <br/>
     * Author: libo <br/>
     * Create Date: 2016年11月29日 <br/>
     * ===============================================================<br/>
     * Modifier: libo <br/>
     * Modify Date: 2016年11月29日 <br/>
     * Modify Description: <br/>
     * ===============================================================<br/>
     *
     * @param ids 选中的ID集合
     * @param res 响应对象
     * @return ResultJson
     * @since JDK 1.7
     */
    @Override
    public ResultJson getFkxxListByIds(String ids, HttpServletResponse res) {
        ResultJson result = new ResultJson(false);
        if (StringUtils.isBlank(ids)) {
            result.setCode(ValidatorConstants.E1600);
            return result;
        }
        String[] idsArr = ids.split(",");
        List<ViewYjfkxx> fkxxViews = tYyxtO2oYjfkxxMapper.getFkxxListByIds(idsArr);
        //code码描述
        Map<String, TYyxtCode> wtlxMap = this.getCodeMapCodeclass(CodeConstants.WTLX);
        Map<String, TYyxtCode> wtztMap = this.getCodeMapCodeclass(CodeConstants.WTZT);
        TYyxtCode tYyxtCode = null;
        ResultJson res1 = null;
        for (ViewYjfkxx fkxx : fkxxViews) {
            //处理状态描述
            tYyxtCode = wtlxMap.get(CodeConstants.WTLX + fkxx.getWtlx());
            if (tYyxtCode != null) {
                fkxx.setWtlxDesc(tYyxtCode.getCodedesc());
            }
            tYyxtCode = wtztMap.get(CodeConstants.WTZT + fkxx.getWtzt());
            if (tYyxtCode != null) {
                fkxx.setWtztDesc(tYyxtCode.getCodedesc());
            }
            fkxx.setTcsjFormat(DateUtils.format(fkxx.getTcsj(), DateUtils.PATTERN_DATETIME));

            //根据附件路径读取问题附件内容信息
            res1 = this.readTxtFromDFS(appConfig.getDfsHost() + "/" + fkxx.getWtnr());
            if (res1 != null && res1.isResult()) {
                //fkxx.setWtnr(res1.getObject().toString());
                fkxx.setWtnr("");
            }
        }

        if (CollectionUtils.isNotEmpty(fkxxViews)) {
            if (StringUtils.isNotBlank(appConfig.getLang())) {
                ExcelUtil.exportTemplate(res, ExcelConstants.YJFKGL_TEMPLATE_PATH, fkxxViews, ExcelConstants.YJFKGL_FILENAME_EN);
            } else {
                ExcelUtil.exportTemplate(res, ExcelConstants.YJFKGL_TEMPLATE_PATH, fkxxViews, ExcelConstants.YJFKGL_FILENAME);
            }

            result.setResult(true);
        }
        return result;
    }

    /**
     *
     * getYjfkxxById,(根据ID获取意见反馈的对象信息). <br/>
     * Author: libo <br/>
     * Create Date: 2016年11月29日 <br/>
     * ===============================================================<br/>
     * Modifier: libo <br/>
     * Modify Date: 2016年11月29日 <br/>
     * Modify Description: <br/>
     * ===============================================================<br/>
     *
     * @param id 意见反馈对象ID
     * @return ViewYjfkxx
     * @since JDK 1.7
     */
    @Override
    public ViewYjfkxx getYjfkxxById(String id) {
        TYyxtO2oYjfkxx yjfkxx = tYyxtO2oYjfkxxMapper.getYjfkxxById(id);
        ViewYjfkxx viewfkxx = new ViewYjfkxx();
        if (yjfkxx != null) {
            viewfkxx.setId(yjfkxx.getId());
            viewfkxx.setTcrdh(yjfkxx.getTcrdh());
            viewfkxx.setTcrjg(yjfkxx.getTcrjg());
            viewfkxx.setTcrmc(yjfkxx.getTcrmc());
            viewfkxx.setTcsj(yjfkxx.getTcsj());
            viewfkxx.setWtlx(yjfkxx.getWtlx());
            viewfkxx.setWtzt(yjfkxx.getWtzt());
            viewfkxx.setFkrmc(yjfkxx.getFkrmc());
            viewfkxx.setFksj(yjfkxx.getFksj());

            //code码描述
            Map<String, TYyxtCode> wtlxMap = this.getCodeMapCodeclass(CodeConstants.WTLX);
            Map<String, TYyxtCode> wtztMap = this.getCodeMapCodeclass(CodeConstants.WTZT);
            TYyxtCode tYyxtCode = null;
            tYyxtCode = wtlxMap.get(CodeConstants.WTLX + yjfkxx.getWtlx());
            if (tYyxtCode != null) {
                viewfkxx.setWtlxDesc(tYyxtCode.getCodedesc());
            }
            tYyxtCode = wtztMap.get(CodeConstants.WTZT + yjfkxx.getWtzt());
            if (tYyxtCode != null) {
                viewfkxx.setWtztDesc(tYyxtCode.getCodedesc());
            }
            //显示日期格式化
            viewfkxx.setTcsjFormat(DateUtils.format(yjfkxx.getTcsj(), DateUtils.PATTERN_DATETIME));

            viewfkxx.setFksjFormat(DateUtils.format(yjfkxx.getFksj(), DateUtils.PATTERN_DATETIME));
            //根据附件路径读取问题附件内容信息
            ResultJson res1 = this.readTxtFromDFS(appConfig.getDfsHost() + "/" + yjfkxx.getWtnr());
            if (res1 != null && res1.isResult()) {
                viewfkxx.setWtnr(res1.getObject().toString());
            }
            //根据附件路径读取反馈附件内容信息
            ResultJson res2 = this.readTxtFromDFS(appConfig.getDfsHost() + "/" + yjfkxx.getFknr());
            if (res2 != null && res2.isResult()) {
                viewfkxx.setFknr(res2.getObject().toString());
            }
        }

        return viewfkxx;
    }

    /**
     *
     * updateYjfkxx,(回复处理意见反馈信息). <br/>
     * Author: libo <br/>
     * Create Date: 2016年11月29日 <br/>
     * ===============================================================<br/>
     * Modifier: libo <br/>
     * Modify Date: 2016年11月29日 <br/>
     * Modify Description: <br/>
     * ===============================================================<br/>
     *
     * @param paramYjfkxx 意见反馈信息对象
     * @return ResultJson
     * @since JDK 1.7
     */
    @Override
    public ResultJson updateYjfkxx(ParamYjfkxx paramYjfkxx, String yhzh, String yhmc) {
        ResultJson result = new ResultJson(false);
        TYyxtO2oYjfkxx record = new TYyxtO2oYjfkxx();
        record.setId(paramYjfkxx.getId());
        //保存UE富文本附件内容到服务器，并返回此附件路径
        if (StringUtils.isNotBlank(paramYjfkxx.getFknr())) {
            ResultJson resultJson = this.saveTxtToDFS(appConfig.getDfsUploadUrl(), paramYjfkxx.getFknr());
            if (resultJson.isResult()) {
                FileBean fb = (FileBean) resultJson.getObject();
                if (fb != null) {
                    record.setFknr(fb.getFilePath());
                } else {
                    record.setFknr("");
                }
            }
        }
        record.setFkrzh(yhzh);
        record.setFkrmc(yhmc);
        record.setFksj(new Date());
        record.setWtzt(CodeConstants.WTZT_YCL);
        record.setDelflag(GlobalConstants.DELFLAG_UPDATE);
        record.setVersions(this.getVersion());
        record.setOperatercode(record.getFkrzh());
        record.setOperatetime(record.getFksj());
        int count = tYyxtO2oYjfkxxMapper.updateYjfkxx(record);
        if (count == 1) {
            result.setResult(true);
        }
        return result;
    }

    /**
     *
     * deleteYjfkxx,(删除意见反馈信息). <br/>
     * Author: libo <br/>
     * Create Date: 2016年11月29日 <br/>
     * ===============================================================<br/>
     * Modifier: libo <br/>
     * Modify Date: 2016年11月29日 <br/>
     * Modify Description: <br/>
     * ===============================================================<br/>
     *
     * @param record 意见反馈信息对象
     * @return ResultJson
     * @since JDK 1.7
     */
    @Override
    public ResultJson deleteYjfkxx(String id, String yhzh) {
        ResultJson result = new ResultJson(false);
        TYyxtO2oYjfkxx record = new TYyxtO2oYjfkxx();
        record.setId(id);
        record.setOperatercode(yhzh);

        record.setDelflag(GlobalConstants.DELFLAG_DELETE);
        record.setVersions(this.getVersion());
        record.setOperatetime(new Date());
        int count = tYyxtO2oYjfkxxMapper.deleteYjfkxx(record);
        if (count == 1) {
            result.setResult(true);
        }
        return result;
    }

    /**
     *
     * getYjfkxxByZh,(拉取该账号下意见数据列表). <br/>
     * Author: libo <br/>
     * Create Date: 2016年12月21日 <br/>
     * ===============================================================<br/>
     * Modifier: libo <br/>
     * Modify Date: 2016年12月21日 <br/>
     * Modify Description: <br/>
     * ===============================================================<br/>
     *
     * @param fkrzh 反馈人账号
     * @param yhtx 用户头像
     * @return List<ViewYjfkxx>
     * @since JDK 1.7
     */
    @Override
    public List<ViewYjfkxx> getYjfkxxByZh(String fkrzh, String yhtx, String ywy) {
        List<ViewYjfkxx> yjfkList = tYyxtO2oYjfkxxMapper.getYjfkxxByZh(null, fkrzh, ywy);

        //处理特殊字段值
        Map<String, TYyxtCode> wtlxMap = this.getCodeMapCodeclass(CodeConstants.WTLX);
        Map<String, TYyxtCode> wtztMap = this.getCodeMapCodeclass(CodeConstants.WTZT);
        TYyxtCode tYyxtCode = null;
        ResultJson res1 = null;
        for (ViewYjfkxx yjfk : yjfkList) {
            tYyxtCode = wtlxMap.get(CodeConstants.WTLX + yjfk.getWtlx());
            if (tYyxtCode != null) {
                yjfk.setWtlxDesc(tYyxtCode.getCodedesc());
            }
            tYyxtCode = wtztMap.get(CodeConstants.WTZT + yjfk.getWtzt());
            if (tYyxtCode != null) {
                yjfk.setWtztDesc(tYyxtCode.getCodedesc());
            }
            yjfk.setTcsjFormat(DateUtils.format(yjfk.getTcsj(), DateUtils.PATTERN_DATETIME));

            //根据附件路径-获取问题内容
            res1 = this.readTxtFromDFS(appConfig.getDfsHost() + "/" + yjfk.getWtnr());
            if (res1 != null && res1.isResult()) {
                yjfk.setWtnr(res1.getObject().toString());
            }
            //根据附件路径-获取反馈内容
            res1 = null;
            res1 = this.readTxtFromDFS(appConfig.getDfsHost() + "/" + yjfk.getFknr());
            if (res1 != null && res1.isResult()) {
                yjfk.setFknr(res1.getObject().toString());
            }
            //会员头像
            yjfk.setYhtx(yhtx);
        }
        return yjfkList;
    }

}
